//
//  XLMusicManager.m
//  QQMusic
//
//  Created by macbook on 16/7/19.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

#import "XLMusicManager.h"
#import <AVFoundation/AVFoundation.h>


@interface XLMusicManager ()

//当前播放的文件名字
@property(nonatomic,copy) NSString *currentFileName;
//播放器
@property (nonatomic,strong) AVAudioPlayer *audioPlayer;


@end

@implementation XLMusicManager

//创建单例对象
static XLMusicManager *instance;
+ (instancetype)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[XLMusicManager alloc]init];
    });
    return instance;
    
}

//播放音乐||继续音乐都是这一个方法 ,所以当调取这个方法时判断一下音乐名字是否相同,如果相同就继续播放
- (void)playMusicWithFileName:(NSString *)filieName
{
    //判断文件名
    if (self.currentFileName != filieName) {
        
        //获取路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:filieName ofType:nil];
#pragma mark - 注意这是fileUrl
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
        //准备播放
        [self.audioPlayer prepareToPlay];
        //赋值数据
        self.currentFileName = filieName;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        //播放音乐
        [self.audioPlayer play];
    });
    
}


//暂停播放
- (void)pauseMusic{

    [self.audioPlayer pause];
}

#pragma mark - 用get和set方法获取歌曲时间

//获取歌曲的持续时间
- (NSTimeInterval)duration
{
    return self.audioPlayer.duration;
}

//获取当前时间

- (NSTimeInterval)currtentTime
{
    return self.audioPlayer.currentTime;
}

//手动调整播放进度
- (void)setCurrtentTime:(NSTimeInterval)currtentTime
{
    self.audioPlayer.currentTime = currtentTime;
}

@end












